//
//  MCNewMarketingEditSKUCell.swift
//  StoreManage
//
//  Created by MC on 2018/6/27.
//  Copyright © 2018年 MC. All rights reserved.
//

import UIKit

class MCNewMarketingEditSKUCell: UITableViewCell {

    var model = MCNewMarketingEditSKUModel() {
        didSet {
            nameLabel.text = "规格:" + model.specName
            moneyTextField.text = String.init(format: "%.2f", CGFloat(model.salePrice.floatValue / 100))
            inventoryTextField.text = model.inventoryNum.stringValue
        }
    }
    
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
        
        self.backgroundColor = UIColor.white
        
        self.addSubview(nameLabel)
        nameLabel.snp.remakeConstraints { (make) ->Void in
            make.top.equalTo(self)
            make.left.equalTo(15)
            make.right.equalTo(-15)
            make.height.equalTo(51)
        }
        
        self.addSubview(lineOneView)
        lineOneView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.height.equalTo(1)
            make.top.equalTo(nameLabel.snp.bottom)
        }
        self.addSubview(moneyLabel)
        moneyLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(lineOneView.snp.bottom)
            make.size.equalTo(CGSize.init(width: 85, height: 50))
        }        
        
        self.addSubview(moneyTextField)
        moneyTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(moneyLabel.snp.right).offset(40)
            make.right.equalTo(-15)
            make.centerY.equalTo(moneyLabel)
            make.height.equalTo(50)
        }

        self.addSubview(lineTwoView)
        lineTwoView.snp.remakeConstraints { (make) ->Void in
            make.left.right.equalTo(self)
            make.height.equalTo(1)
            make.top.equalTo(moneyTextField.snp.bottom)
        }
        
        self.addSubview(inventoryLabel)
        inventoryLabel.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(15)
            make.top.equalTo(lineTwoView.snp.bottom)
            make.size.equalTo(CGSize.init(width: 85, height: 50))
        }
        
        self.addSubview(inventoryTextField)
        inventoryTextField.snp.remakeConstraints { (make) ->Void in
            make.left.equalTo(moneyLabel.snp.right).offset(40)
            make.right.lessThanOrEqualTo(-15)
            make.centerY.equalTo(inventoryLabel)
            make.height.equalTo(50)
        }
        self.addSubview(lineThreeView)
        lineThreeView.snp.remakeConstraints { (make) ->Void in
            make.left.bottom.right.equalTo(self)
            make.height.equalTo(7)
        }
        
    }
    required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
        
    }
    
    //键盘的隐藏
    @objc func keyBoardWillHide(_ notification: Notification){

    }

    lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 16)
        label.textColor = UIColor.MCWords
        label.numberOfLines = 0
        return label
    }()
    
    lazy var lineOneView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var lineTwoView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()
    
    lazy var lineThreeView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.MCGray_light
        return view
    }()

    lazy var moneyLabel: UILabel = {
        let label = UILabel()
        label.text = "活动价格(¥)"
        label.font = MCFont(14)
        label.textColor = UIColor.MCGray_middle
        return label
    }()

    lazy var moneyTextField: MCTextFiled = {
        let tf = MCTextFiled()
        tf.placeholder = "请输入活动价格"
        tf.keyboardType = UIKeyboardType.decimalPad
        tf.returnKeyType = UIReturnKeyType.done
        tf.delegate = self
        return tf
    }()
    
    lazy var inventoryLabel: UILabel = {
        let label = UILabel()
        label.text = "活动库存(件)"
        label.font = MCFont(14)
        label.textColor = UIColor.MCGray_middle
        return label
    }()

    lazy var inventoryTextField: MCTextFiled = {
        let tf = MCTextFiled()
        tf.placeholder = "请输入活动库存"
        tf.keyboardType = UIKeyboardType.numberPad
        tf.returnKeyType = UIReturnKeyType.done
        tf.delegate = self
        return tf
    }()

}

extension MCNewMarketingEditSKUCell : UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        let sale : Double = Double(moneyTextField.text ?? "")! * 100
        let salePrice : Int = Int.init(sale)
        model.salePrice = NSNumber.init(value: salePrice)
        model.inventoryNum = NSNumber.init(value: Int(inventoryTextField.text!)!)
    }
    
}
